<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { numberApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  const $emit = defineEmits(['updateList'])
  const dataForm = ref();
  const action = reactive({
    dialogFormVisible:false,
  })
  const form = reactive({
    user_id:'',
    nickname:''
  })
  const formRules = {
    nickname: [{ required: true, trigger: 'blur', message: '昵称不可为空' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async (valid:any) => {
      if(valid) {
        const loading = Toastloading()
        await numberApi.setNickname(form.user_id, form)
        ToastSuccess('修改成功')
        loading.close()
        closeDialog()
        $emit('updateList')
      }
    })

  }
  const openDialog = (query:any = {})=> {
    const { user_id, nickname } = query;
    form.nickname = nickname;
    form.user_id = user_id;
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    Object.assign(form,{
      user_id:'',
      nickname:''
    })
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="20vh"
    class="xm-dialog"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="false"
    :show-close="false"
    :close-on-press-escape="false"
    destroy-on-close
    width="30%"
    title="设置昵称">
      <el-form ref="dataForm" :model="form" :rules="formRules" label-width="70px">
        <el-form-item label="昵称" prop="nickname" >
          <el-input v-model="form.nickname" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
